﻿using AutoMapper;
using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.Traceabilitys.DeepProcessings;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.Traceabilitys.DeepProcessings
{
    /// <summary>
    /// 添加深加工信息命令处理
    /// </summary>
    public class AddDeepProcessingCommandHandlter : IRequestHandler<AddDeepProcessingCommand, ApiResult<int>>
    {
        /// <summary>
        /// 深加工仓储
        /// </summary>
        private readonly IBaseRepository<DeepProcessingModel> deepProcessingRep;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="deepProcessingRep">深加工仓储</param>
        /// <param name="mapper">映射</param>
        public AddDeepProcessingCommandHandlter(IBaseRepository<DeepProcessingModel> deepProcessingRep ,IMapper mapper)
        {
            this.deepProcessingRep = deepProcessingRep;
            this.mapper = mapper;
        }


        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<int>> Handle(AddDeepProcessingCommand request, CancellationToken cancellationToken)
        {

            //新增深加工信息
             var  deepProcessing=mapper.Map<DeepProcessingModel>(request);
             deepProcessing.Creatime=DateTime.Now;

             await deepProcessingRep.AddAsync(deepProcessing);


            return new ApiResult<int>
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "新增深加工信息成功"

            };
        }
    }
}
